/*
 * @Date: 2020-10-26 12:00:51
 * @LastEditors: E'vils
 * @LastEditTime: 2021-03-15 11:52:44
 * @Description: 
 * @FilePath: /src/pages/mine/mine.js
 */
/* eslint-disable no-undef */
//index.js
//获取应用实例
const app = getApp();
import config from "../../config";
import { wxShowMaskLoading } from "../../utils/promisify";
import { serializePathQuery } from "../../utils/api/http";
import Card from "./card";
import {shareQrCode} from "../../utils/api/course";
Page({
    imagePath: "",
    history: [],
    future: [],
    isSave: false,
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        ImgUrl: app.globalData.ImgUrl,
        displayArea: app.globalData.displayArea,
        gradeList: app.globalData.gradeList,
        shareWindow:false, // 分享弹窗
        customActionStyle: {
            border: {
                borderColor: "#1A7AF8",
            },
            scale: {
                textIcon: "/palette/switch.png",
                imageIcon: "/palette/scale.png"
            },
            delete: {
                icon: "/palette/close.png"
            }
        },
        settingList: [
            {
                name: "情侣课表",
                icon: "/images/couplesSchedule.png",
                url: "/pages/couples/couples?",
            },
            {
                name: "设置课表背景",
                icon: "/images/scheduleIcon.png",
                url: "/pages/schedule/schedule?",
            },
            {
                name: "导入课表",
                icon: "/images/schedule.png",
                url: "/pages/importTable/importTable?",
            },
            {
                name: "设置",
                icon: "/images/setting.png",
                url: "/pages/setting/setting?",
            },

        ],
        userInfo: app.globalData.userInfo,
        hasUserInfo: app.globalData.userInfo,
    },
    onImgOK(e) {
        this.imagePath = e.detail.path;
        this.setData({
            image: this.imagePath
        });
        wx.hideLoading();
        if (this.isSave) {
            this.saveImage(this.imagePath);
        }
    },
    onShareAppMessage: function (res) {
        let {user_id,nickname} = app.globalData.userInfo;
        if (res.from === "button") {
            // 来自页面内转发按钮
            console.log(res.target);
        }
        this.hideshareWindow();
        return {
            title:  nickname + "向你分享了一份课表",
            imageUrl:"/images/shareSchedule.png",
            path: "/pages/tablePreview/tablePreview?u=" + user_id
        };
    },
    onLoad: function () {
        app.userInfoReadyCallback = (res) => {
            this.setData({
                userInfo: res,
                hasUserInfo: true,
            });
        };
    },
    onShow() {
        if (app.globalData.userInfo) {
            let settingList = this.data.settingList;
            // 检查是否项目管理,增加设置首页背景
            if (app.globalData.userInfo.admin_user_id == app.globalData.userInfo.user_id && settingList.length <= 5) {
                settingList.push({
                    name: "设置首页背景",
                    icon: "/images/scheduleIcon.png",
                    url: "/pages/indexSchedule/indexSchedule?",
                });
            }
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true,
                settingList
            });
        }
    },
    bindGetUserInfo(e) {
        if (e.detail.userInfo) {
            wxShowMaskLoading("登陆中");
            app.getInfo().then((v) => {
                console.log(v, "getInfo");
                wx.hideLoading();
                this.setData({
                    userInfo: v,
                    hasUserInfo: true,
                });
            });
        }
    },
    /**
     * 跳转到新页面设置
     * @param {*} url 传入的跳转地址
     */
    navigationTo(e) {
        let url = e.target.dataset.url;
        let { lovers_id } = this.data.userInfo;
        if (url == "shareSchedule") {
            this.setData({
                shareWindow:true
            },()=>{
                if (this.imagePath && typeof this.imagePath === "string") {
                    // this.setData({
                    //     paintPallette: this.imagePath,
                    // });
                } else {
                    wx.showLoading({
                        title: "海报生成中",
                    });
                    shareQrCode({
                        page:"pages/tablePreview/tablePreview",
                        // page:"pages/index/index",
                        scene:`u=${this.data.userInfo.user_id}`
                    }).then(res=>{
                        console.log("res", res.data.file);
                        this.setData({
                            paintPallette: new Card().palette("/images/shareSchedule.png",res.data.file),
                        });
                    });
                }
            });

            return;
        }
        if (url == "/pages/couples/couples?") {
            let couplesId = lovers_id ? lovers_id : null;
            wx.navigateTo({
                url:
                    url +
                    serializePathQuery({
                        couplesId,
                    }),
            });
        } else {
            wx.navigateTo({
                url,
            });
        }
    },
    /**
     * 隐藏分享窗口
     */
    hideshareWindow(){
        this.setData({
            shareWindow:false
        });
    },
    /**
     * 跳转其他小程序
     */
    jumpApplet(){
        wx.navigateToMiniProgram({
            appId: "wxa8868572415a6b33", // 练手lab
            path: "page/index/index",
            extraData: {
                inviter: config.inviter // inviter后面的数字代表你在练手Lab里面的用户id，请修改成你的，在练手Lab小程序我的里面获取
            },
        });
    },
    savePicture(){
        if (this.imagePath && typeof this.imagePath === "string") {
            this.isSave = false;
            wx.saveImageToPhotosAlbum({
                filePath: this.imagePath,
                success:() =>{
                    this.hideshareWindow();
                }
            });
        }
    },
});
